import { defineStore } from 'pinia'
import {ref} from 'vue'
import {getAddressList,addAddressList,updateAddressList,deleteAddressList} from '@/api/address.js'
export const addressStore=defineStore('addressStore',()=>{
  const address=ref([])
  const getAddress=async ()=>{
    const {result:res}=await getAddressList()
    address.value=res
  }
  // 添加地址
  const addAddress=async (data)=>{
    await addAddressList(data)
    await getAddress()
  }
  // 修改地址
  const updateAddress=async (id,data)=>{
    await updateAddressList(id,data)
    await getAddress()
  }
  // 清空地址
  const clearAddress=()=>{
    address.value=[]
  }
  //  删除地址
  const deleteAddress=async (id)=>{
    await deleteAddressList(id)
    await getAddress()
  }
  return {
    address,
    getAddress,
    addAddress,
    updateAddress,
    clearAddress,
    deleteAddress
  }
},
{
  persist:true
})